﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace GTP.AppFrame.PrototypeTools.Common
{
    /// <summary>
    /// 生成时通用的方法
    /// </summary>
    public static class GeneratorUtility
    {
        /// <summary>
        /// 配置的元数据，包括：业务表单，关系图、列表页、编辑页
        /// </summary>
        public enum ConfigMetadataName
        {
            BizForm,
            Diagram,
            ListPage,
            EditPage
        }
        /// <summary>
        /// 根据名称读取元数据
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static string ReadConfigMetadata(ConfigMetadataName name)
        {
            string result = string.Empty;
            string fileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "GTPMetadata");
            switch (name)
            {
                case ConfigMetadataName.BizForm:
                    fileName = Path.Combine(fileName, "bizform.xml");
                    break;
                case ConfigMetadataName.Diagram:
                    fileName = Path.Combine(fileName, "diagram.xml");
                    break;
                case ConfigMetadataName.ListPage:
                    fileName = Path.Combine(fileName, "listpage.xml");
                    break;
                case ConfigMetadataName.EditPage:
                    fileName = Path.Combine(fileName, "editpage.xml");
                    break;
                default:
                    break;
            }
            if (File.Exists(fileName))
            {
                result = ReadFile(fileName);
            }
            return result;
        }
        /// <summary>
        /// 把生成的元数据信息写入文件
        /// </summary>
        /// <param name="name"></param>
        /// <param name="data"></param>
        public static void WriteMetadata(string name, string data)
        {
            string fileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Output");
            fileName = Path.Combine(fileName, DateTime.Now.ToString("yyyyMMddhhmmss"));
            if (!Directory.Exists(fileName))
            {
                Directory.CreateDirectory(fileName);
            }
            fileName = fileName + "\\" + name;
            WriteFile(data, fileName);
        }
        public static string ReadConfigEntityField(EntityProperyType type)
        {
            string result = string.Empty;
            string fileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "GTPMetadata");
            fileName = Path.Combine(fileName, "Property");
            switch (type)
            {
                case EntityProperyType.Text:
                    fileName = Path.Combine(fileName, "textfield.xml");
                    break;
                case EntityProperyType.Date:
                    fileName = Path.Combine(fileName, "datefield.xml");
                    break;
                case EntityProperyType.Lookup:
                    fileName = Path.Combine(fileName, "lookupfield.xml");
                    break;
                case EntityProperyType.Column:
                    fileName = Path.Combine(fileName, "column.xml");
                    break;
                default:
                    break;
            }
            if (File.Exists(fileName))
            {
                result = ReadFile(fileName);
            }
            return result;
        }
        #region Private Method
        /// <summary>
        /// 读文件
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        private static string ReadFile(string fileName)
        {
            string result = string.Empty;
            using (StreamReader reader = new StreamReader(fileName))
            {
                result = reader.ReadToEnd();
            }
            return result;
        }
        /// <summary>
        /// 写文件
        /// </summary>
        /// <param name="data"></param>
        /// <param name="fileName"></param>
        private static void WriteFile(string data, string fileName)
        {
            using (StreamWriter writer = new StreamWriter(fileName))
            {
                writer.Write(data);
            }
        }
        #endregion
    }
}
